import request from '@/utils/request';
import { OptionTypes, ResponseData, SUCCESS_CODE } from '@/utils/types.d';
import {
  CreateScheduleType,
  CreateShiftConfigType,
  PerformanceRecordTableItem,
  ScheduleManagementTableItem,
  ShiftConfigTableItem,
  UpdateScheduleType,
  UpdateShiftConfigType,
} from './types.d';

export async function queryPerformanceList(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: PerformanceRecordTableItem[];
}> {
  const res: ResponseData<{ total: number; data: PerformanceRecordTableItem[] }> = await request(
    '/api/cashier-manager/v1.0.0/performance/list',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

// ShiftConfigTableItem
export async function queryShiftConfigs(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ShiftConfigTableItem[];
}> {
  const res: ResponseData<{ total: number; data: ShiftConfigTableItem[] }> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/shifts',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createShiftConfig(params: CreateShiftConfigType) {
  const res = await request('/api/cashier-manager/v1.0.0/schedule-management/shift', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}

export async function updateShiftConfig(newInfo: UpdateShiftConfigType) {
  if (!newInfo || Object.keys(newInfo).length < 2)
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  const res: ResponseData<null> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/shift',
    {
      method: 'PUT',
      data: newInfo,
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '更新成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function removeShiftConfig(id: string) {
  const res: ResponseData<null> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/shift',
    {
      method: 'DELETE',
      data: {
        id,
      },
    },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}

export async function getShifts(name: string, ownerId?: string): Promise<OptionTypes[]> {
  const res: ResponseData<OptionTypes[]> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/shiftnames',
    {
      params: { name, ownerId },
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return res.data;
  }
  return [];
}

export async function queryScheduleManagement(
  data: any,
  sort: any,
): Promise<{
  success: boolean;
  total: number;
  data: ScheduleManagementTableItem[];
}> {
  const res: ResponseData<{ total: number; data: ScheduleManagementTableItem[] }> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/schedules',
    {
      params: {
        data,
        sort,
      },
    },
  );
  if (res?.code === SUCCESS_CODE)
    return { success: true, total: res.data.total, data: res.data.data };
  return { success: false, total: 0, data: [] };
}

export async function createSchedule(params: CreateScheduleType) {
  const res = await request('/api/cashier-manager/v1.0.0/schedule-management/schedule', {
    method: 'POST',
    data: params,
  });
  if (res?.code === SUCCESS_CODE)
    return {
      data: res.data,
      success: true,
      message: '添加成功',
    };
  return { data: res?.data, success: false, message: res?.message || '添加失败' };
}

export async function updateSchedule(newInfo: UpdateScheduleType) {
  if (!newInfo || Object.keys(newInfo).length < 2)
    return { success: false, message: '更新失败，请填入需要更新的信息' };
  const res: ResponseData<null> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/schedule',
    {
      method: 'PUT',
      data: newInfo,
    },
  );
  if (res?.code === SUCCESS_CODE) {
    return { success: true, data: res.data, message: '更新成功' };
  }
  return { success: false, data: res?.data, message: res?.message || '更新失败' };
}

export async function removeSchedule(id: string) {
  const res: ResponseData<null> = await request(
    '/api/cashier-manager/v1.0.0/schedule-management/schedule',
    {
      method: 'DELETE',
      data: {
        id,
      },
    },
  );
  if (res?.code === SUCCESS_CODE) return { success: true, message: '删除成功' };
  return { success: false, message: res?.message || '删除失败' };
}
